﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Data;

namespace Dooshion.DAL
{
    /// <summary>
    /// 用于对业务实体类的创建、缓存等操作的工厂类
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class CreateEntity<T>
    {
        /// <summary>
        /// 创建业务实体类实例
        /// </summary>
        /// <param name="entityName">实体类名称</param>
        /// <returns>泛型实体类型</returns>
        public static T Create(string entityName)
        {
            return (T)Assembly.Load("Dooshion.Model").CreateInstance("Dooshion.Model." + entityName);
        }

        /// <summary>
        /// 根据DataTable 创建业务实体集合
        /// </summary>
        /// <typeparam name="T">泛型实体类型</typeparam>
        /// <param name="dt">DataTable数据集</param>
        /// <returns>泛型业务实体集合</returns>
        public List<T> CreateEntityCollection<T>(DataTable dt)
        {
            List<T> entitylist = new List<T>();
            if (dt == null || dt.Rows.Count == 0)
            {
                return entitylist;
            }
            Type objType = typeof(T);
            foreach (DataRow dr in dt.Rows)
            {
                object obj = System.Activator.CreateInstance(objType);//创建实例  
                foreach (System.Reflection.PropertyInfo pi in objType.GetProperties())//遍历T类的所有属性  
                {
                    if (pi.PropertyType.IsPublic
                        && pi.CanWrite
                        && dt.Columns.Contains(pi.Name))//属性是否为public和可写  
                    {
                        Type pType = Type.GetType(pi.PropertyType.FullName);//属性类型  
                        //Type genericTypeDefinition = pType.GetGenericTypeDefinition(); 

                        object value;
                        if (pType.Name == "Nullable`1")
                        {
                            if (!String.IsNullOrEmpty(dr[pi.Name].ToString()))
                            {
                                value = Convert.ChangeType(dr[pi.Name], Nullable.GetUnderlyingType(pType));
                            }
                            else
                            {
                                value = null;
                            }
                        }
                        //Convert.ChangeType(dr[pi.Name], pType)
                        else
                        {
                            if (!String.IsNullOrEmpty(dr[pi.Name].ToString()))
                            {
                                value = Convert.ChangeType(dr[pi.Name], pType);//转换dt值为对应属性的类型  
                            }
                            else
                            {
                                value = null;
                            }
                        }


                        objType.GetProperty(pi.Name).SetValue(obj, value, null);//赋值  
                    }
                }
                entitylist.Add((T)obj);
            }
            return entitylist;
        }


    }
}
